<template>
  <el-carousel :interval="3000" type="card" height="400px" class="banner">
    <el-carousel-item v-for="banner in banner_list" :key="banner.title">
      <router-link :to="banner.link">
        <img :src="banner.image.replace('http://127.0.0.1:8000',banner_url)" :title="banner.title" />
      </router-link>
    </el-carousel-item>
  </el-carousel>
</template>

<script>
export default {
  name: "Banner",
  data() {
    return {
      banner_list: [],
      banner_url: "",
      token: {
        jwt: "jwt" + " " + this.$cookies.get("token"),
      },
    };
  },
  activated() {
    this.get_banner();
  },
  methods: {
    get_banner() {
      if (this.$cookies.get("token") != null) {
        this.$axios({
          url: this.$settings.base_url + "/home/banners/",
          method: "get",
          headers: {
            authorization: this.token.jwt,
          },
        })
          .then((response) => {
            this.banner_url = this.$settings.base_url;
            this.banner_list = response.data.results;
          })
          .catch(() => {
            this.$notify({
              type: "error",
              title: "访问异常,请联系陈鸿龙",
              duration: 3000,
            });
          });
      } else {
        this.$axios({
          url: this.$settings.base_url + "/home/banners/",
          method: "get",
        })
          .then((response) => {
            this.banner_url = this.$settings.base_url;
            this.banner_list = response.data.results;
          })
          .catch(() => {
            this.$notify({
              type: "error",
              title: "访问异常,请联系陈鸿龙",
              duration: 3000,
            });
          });
      }
    },
  },
};
</script>

<style>
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
.banner img {
  width: 100%;
  height: 100%;
}
.banner {
  /* background-color: #d3dce6; */
  background-image: url("../assets/img/timg.jpg");
}
</style>


